/**
		columns:[[
			{field:'status',title:'用户状态',width:150,align:'center',formatter:function(value,rec){
				if(value == 1)return "<span style='color:blue;'>正常<span>";
				else return "<span style='color:red;'>禁用<span>";
			}},
			{field:'checkStatus',title:'信息审核状态',width:150,align:'center',formatter:function(value,rec){
				if(value == 1)return "<span style='color:blue;'>未提交审核<span>";
				else return "<span style='color:red;'>提交审核<span>";
			}},
			{field:'creater',title:'添加者',width:200,align:'center',formatter:function(value,rec){
				return rec.createrId==0? '系统内置':value;
			}, hidden: hidden},
		]]
 */
(function($) {
	$.fn.extend({
		DataGrid: function(options) {
			var settings = $.extend({
				url: null,
				columns: [],//上下文
				width:'auto',
				height:'auto',
				pagination:true,
				pageLength: 5,
				pageSize:10,
				sortName: null,
				sortOrder: null,
				current: 1,
				fitColumns:true,
				onBeforeLoad:function(param){return true;},
				onLoadSuccess:function(total,rows){
					
				}
			}, options);
			var requestDataAndBuild = function(current, param){
				$.tipbox('正在加载数据，请稍后...','loading',{modal:true});
				$.post(settings.url, param, function(data){
					$.closeTipbox();
					if(typeof data == 'string')data = eval('('+ data +')');
					var total = $.isArray(data)?data.length: data.total;
					var rows = $.isArray(data)?data: data.rows;
					innerHtml = "";
					for(var x in rows){
						innerHtml += "<tr>";
						var row = rows[x];
						for(var x in settings.columns){
							var column = settings.columns[x];
							if(column.formatter && $.isFunction(column.formatter)){
								var h = column.formatter(row[column.field],row,x);
								innerHtml += '<td field="'+ column.field +'" align="'+(column.align?column.align:'left')+'">'+h+'</td>';
								break;
							}
							innerHtml += '<td field="'+ column.field +'" align="'+(column.align?column.align:'left')+'">'+row[column.field]+'</td>';
						}
						innerHtml += "</tr>";
					}
					
					$target.find('tr[mark!=title]').remove();
					$target.find('tr[mark=title]').parent().append(innerHtml);
					settings.onLoadSuccess(total,rows);
					
					//分页条
					if(settings.pagination){
						var numPager = Math.ceil(total/settings.pageSize);
						var interval = getInterval(settings.pageLength, current, numPager);
						$pagination = $target.find('.pagination').empty();
						$pagination.append('Page <span class="red">'+current+'</span> &nbsp;of &nbsp;'+numPager+' &nbsp; &nbsp;');
						if(current == 1 || numPager==0){
							$pagination.append(' &nbsp;<span style="color: #375B91;">首页</span>');
							$pagination.append(' &nbsp;<span style="color: #375B91;">前一页</span>');
						}else {
							$pagination.append(createAForPager('首页',1));
							$pagination.append(createAForPager('前一页',current-1));
						}
						if(numPager == 0){
							$pagination.append(' &nbsp;<span class="red">1</span>');
						}else{
							if(interval[0] == interval[1]){
								$pagination.append(' &nbsp;<span class="red">'+interval[0]+'</span>');
							}else{
								if(interval[0] != 1){
									$pagination.append(' ... ');
								}
								for(var i=interval[0];i<=interval[1];i++){
									if(i == current)$pagination.append(' &nbsp;<span class="red">'+i+'</span>');
									else $pagination.append(createAForPager(i,i));
								}
								if(interval[1] != numPager){
									$pagination.append(' ... ');
								}
							}
						}
						if(current == numPager || numPager==0){
							$pagination.append(' &nbsp;<span style="color: #375B91;">下一页</span>');
							$pagination.append(' &nbsp;<span style="color: #375B91;">尾页</span>');
						}else{
							$pagination.append(createAForPager('下一页',current+1));
							$pagination.append(createAForPager('尾页',numPager));
						}
						
						//$target.find('.pagination').html(innerHtml);
					}
					
				});
			};
			
			/**
			 * 极端分页的起始和结束点，这取决于 当前页 和 显示条目数.
			 * @返回 {数组(Array)}
			 */
			var getInterval = function(showLength, current, np) {
				var ne_half = Math.ceil(showLength/2);
				var upper_limit = np - showLength;
				var start = current > ne_half? Math.max(Math.min(current-ne_half+1, upper_limit+1), 1):1;
				var end = current > ne_half? Math.min(current+ne_half-1, np):Math.min(showLength, np);
				return [start,end];
			};
			
			var createAForPager = function(text, page){
				return $('<a/>',{title:text, page:page, text:text,'href':'javascript:void(0)',click:function(ev){
					var param = {'page': page};
					if(settings.pageSize)param['rows']=settings.pageSize;
					if(settings.sortName != null)param['sort']=settings.sortName;
					if(settings.sortOrder != null)param['order']=settings.sortOrder;
					settings.onBeforeLoad(param);
					requestDataAndBuild(page, param);
				}});
			};
			var reload = function(){
				var param = {'page':1};
				if(settings.pageSize)param['rows']=settings.pageSize;
				if(settings.sortName != null)param['sort']=settings.sortName;
				if(settings.sortOrder != null)param['order']=settings.sortOrder;
				settings.onBeforeLoad(param);
				requestDataAndBuild(1, param);
			};
			if(typeof options == 'string'){
				if(options == 'reload'){
					settings = $target.data('settings');
					return reload();
				}
			}
			
			return this.each(function() {
				$target = $(this);
				$target.data('settings',settings);
				var innerHtml = '<table cellspacing="0" rules="all" border="1" style="border-collapse: collapse;">';
				innerHtml += '<tr mark="title">';
				for(var x in settings.columns){
					var column = settings.columns[x];
					var width = (column.width? 'width="'+column.width+'"':'');
					innerHtml += '<th scope="col" '+width+' align="'+(column.align?column.align:'left')+'">'+column.title+'</th>';
				}
				innerHtml += '</tr></table>';
				if(settings.pagination){
					innerHtml += '<div class="pagination">--无数据--</div>';
				}
				$target.html(innerHtml);
				var current = settings.current;
				if(settings.url != null){
					var param = {'page':current};
					if(settings.pageSize)param['rows']=settings.pageSize;
					if(settings.sortName != null)param['sort']=settings.sortName;
					if(settings.sortOrder != null)param['order']=settings.sortOrder;
					settings.onBeforeLoad(param);
					requestDataAndBuild(current, param);
				}
				
				
			});
		}
	});
	
})(jQuery);

(function($){
	jQuery.extend({
		tipbox: function(message, icon, opts){//'success'、'warning'、'error'、'loading',info
			var options = $.extend({
				close: function(){},
				modal: false,
				timeout: 2000
			}, opts || {});
			$('body').find('#my_tipbox').remove();
			var box = $('<div/>',{id:'my_tipbox','class': 'my_tipbox_layer_wrap'});
			var tip = $('<span/>',{'class': 'my_tipbox_layer'}).css('z-index',10000);
			switch(icon){
			case 'success':
				$('<span/>',{'class': 'gtl_ico_succ'}).appendTo(tip);
				break;
			case 'warning':
				$('<span/>',{'class': 'gtl_ico_hits'}).appendTo(tip);
				break;
			case 'error':
				$('<span/>',{'class': 'gtl_ico_fail'}).appendTo(tip);
				break;
			case 'loading':
				$("<span class='gtl_ico_clear'></span><span class='loading'></span>").appendTo(tip);
				break;
			default:
				$('<span/>',{'class': 'gtl_ico_clear'}).appendTo(tip);
			break;
			}
			tip.append($('<span/>',{text:' '+message, id:'tip_message'})).append('<span class="gtl_end"></span>');
			box.append(tip);
			var top = ($(window).height() - 50) / 2 +"px";
			box.css('top',top).appendTo('body');
			if(options.modal){
				$('<div/>',{'class': 'my_tipbox_model'}).appendTo('body');
			}
			if(icon != 'loading'){
				options.timeout = options.timeout > 0? options.timeout:2000;
				setTimeout(function(){
					$.closeTipbox(options.close);
				}, options.timeout);
			}
		},
		closeTipbox: function(callback){
			$('body').find('#my_tipbox').fadeOut("normal",function(){
				$('.my_tipbox_model').remove();
				$(this).remove();
				if(callback)callback();
			});
		}
	});
})(jQuery);